from PySide6.QtWidgets import QLineEdit

from test_tools.script.excel_tools_datas import ExcelToolsData
from test_tools.script.path_tool import *


def print_path():
    """输出脚本和程序工作目录"""
    log("==========输出脚本和程序工作目录==========")
    current_dir = os.path.dirname(os.path.abspath(__file__))
    working_dir = os.getcwd()
    log(f"当前脚本 {os.path.basename(__file__)} 运行目录： {current_dir}")
    log(f"当前程序工作目录：{working_dir}")
    log("======================================")


class ExcelToolsView:
    # noinspection PyTypeChecker
    def __init__(self, _app):
        print_path()
        self.__message = ExcelToolsMessageView()
        self.__app = _app
        self.__excel_tools_data = None
        self.__excel_tools_ui = None
        self.__print_worker = None
        # ---------tab1控件---------
        self.__color_menu: QMenu  # 主题菜单
        self.__high_menu: QMenu  # 高亮菜单
        self.__tabWidget: QTabWidget  # tab标签
        self.__up_server_index: QComboBox  # 上传服务器索引
        self.__server_config_index: QComboBox  # 服务器配置索引
        self.__export_dir_index: QComboBox  # 导出目录索引
        self.__is_export_excel: QCheckBox  # 是否导出excel
        self.__is_up_data: QCheckBox  # 是否自动上传配置
        self.__is_down_config: QCheckBox  # 是否下载配置
        self.__is_copy_file: QCheckBox  # 是否复制配置文件到工程
        self.__server_config_list: QListWidget  # Nacos登陆配置列表
        self.__log_text: QTextEdit  # 日志窗口
        self.__start_Button: QPushButton  # 开始按钮
        self.__push_Button: QPushButton  # 上传按钮
        self.__log_scrollbar = None  # 日志滚动条
        # ---------tab2控件---------
        self.__up_server_index_2: QComboBox  # 上传服务器索引
        self.__server_config_index_2: QComboBox  # 服务器配置索引
        self.__svn_old_index: QLineEdit  # svn旧索引
        self.__svn_new_index: QLineEdit  # svn新索引
        self.__is_copy_file_2: QCheckBox  # 是否复制配置文件到工程
        self.__is_zip_file: QCheckBox  # 是否压缩文件
        self.__hot_export_Button: QPushButton  # 热更导表开始按钮
        self.__push_zip_Button: QPushButton  # 热更zip文件上传按钮
        self.__log_text_2: QTextEdit  # 日志窗口
        self.__log_scrollbar_2 = None  # 日志滚动条
        self.__up_zip_index: QComboBox  # zip文件索引
        self.__is_uPdata_accessToken: QCheckBox  # 是否更新 nacos accessToken
        self.__is_uPdata_svn: QCheckBox  # 是否更新 SVN旧节点
        # ---------初始化---------
        self.__init_view()  # 初始化控件
        self.__init_event()  # 初始化事件
        self.__init_threadWorker()  # 初始化线程
        self.__init_excel_tools_data()  # 初始化ExcelToolsData
        self.__init_data()  # 初始化数据
        self.__init_panel()  # 初始化面板
        self.__init_style()  # 初始化样式
        self.__init_menuBar()  # 初始化菜单栏

    # noinspection PyUnresolvedReferences
    def __init_view(self):
        log("ExcelToolsView-> __init_view")
        try:
            # --------------------tal1-----------------
            self.__excel_tools_ui = QUiLoader().load(excel_tools_view_ui_path)
            self.__color_menu: QMenu = self.__excel_tools_ui.color_menu
            self.__high_menu: QMenu = self.__excel_tools_ui.high_menu
            self.__tabWidget = self.__excel_tools_ui.tabWidget
            self.__tabWidget.setTabText(0, "普通导表")
            self.__tabWidget.setTabText(1, "热更导表")
            self.__up_server_index = self.__excel_tools_ui.up_server_index
            self.__server_config_index = self.__excel_tools_ui.server_config_index
            self.__export_dir_index = self.__excel_tools_ui.export_dir_index
            self.__is_export_excel = self.__excel_tools_ui.is_export_excel
            self.__is_up_data = self.__excel_tools_ui.is_up_data
            self.__is_down_config = self.__excel_tools_ui.is_down_config
            self.__is_copy_file = self.__excel_tools_ui.is_copy_file
            self.__server_config_list = self.__excel_tools_ui.server_config_list
            self.__log_text = self.__excel_tools_ui.log_text
            self.__log_scrollbar = self.__log_text.verticalScrollBar()
            self.__start_Button = self.__excel_tools_ui.start_Button
            self.__push_Button = self.__excel_tools_ui.push_Button
            # --------------------tal2-----------------
            self.__up_server_index_2 = self.__excel_tools_ui.up_server_index_2
            self.__server_config_index_2 = self.__excel_tools_ui.server_config_index_2
            self.__svn_old_index = self.__excel_tools_ui.svn_old_index
            self.__svn_new_index = self.__excel_tools_ui.svn_new_index
            self.__is_copy_file_2 = self.__excel_tools_ui.is_copy_file_2
            self.__is_zip_file = self.__excel_tools_ui.is_zip_file
            self.__hot_export_Button = self.__excel_tools_ui.hot_export_Button
            self.__log_text_2 = self.__excel_tools_ui.log_text_2
            self.__log_scrollbar_2 = self.__log_text_2.verticalScrollBar()
            self.__push_zip_Button = self.__excel_tools_ui.push_zip_Button
            self.__push_zip_Button.setEnabled(False)
            self.__up_zip_index = self.__excel_tools_ui.up_zip_index
            self.__up_zip_index.setEnabled(False)
            self.__is_uPdata_accessToken = self.__excel_tools_ui.is_uPdata_accessToken
            self.__is_uPdata_svn = self.__excel_tools_ui.is_uPdata_svn
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化窗体界面 异常： {e}")

    def __init_event(self):
        """初始化绑定事件"""
        log("ExcelToolsView-> __init_event")
        try:
            self.__tabWidget.currentChanged.connect(self.tabWidget_event)
            # --------------------tal1-----------------
            self.__up_server_index.currentIndexChanged.connect(self.up_server_index_event)
            self.__server_config_index.currentIndexChanged.connect(self.server_config_index_event)
            self.__export_dir_index.currentIndexChanged.connect(self.export_dir_index_event)
            self.__is_export_excel.clicked.connect(self.is_export_excel_event)
            self.__is_up_data.clicked.connect(self.is_up_data_event)
            self.__is_down_config.clicked.connect(self.is_down_config_event)
            self.__is_copy_file.clicked.connect(self.is_copy_file_event)
            self.__start_Button.clicked.connect(self.start_Button_event)
            self.__push_Button.clicked.connect(self.push_Button_event)
            self.__color_menu.triggered[QAction].connect(self.color_menu_event)
            self.__color_menu.hovered[QAction].connect(self.color_menu_hove_event)
            self.__color_menu.aboutToHide.connect(self.color_menu_hide_event)
            self.__high_menu.triggered[QAction].connect(self.high_color_event)
            # --------------------tal2-----------------
            self.__up_server_index_2.currentIndexChanged.connect(self.up_server_index_2_event)
            self.__server_config_index_2.currentIndexChanged.connect(self.server_config_index_2_event)
            self.__svn_old_index.editingFinished.connect(self.svn_old_index_event)
            self.__svn_new_index.editingFinished.connect(self.svn_new_index_event)
            self.__is_copy_file_2.clicked.connect(self.is_copy_file_2_event)
            self.__is_zip_file.clicked.connect(self.is_zip_file_event)
            self.__hot_export_Button.clicked.connect(self.hot_export_Button_event)
            self.__push_zip_Button.clicked.connect(self.push_zip_Button_event)
            self.__up_zip_index.currentIndexChanged.connect(self.up_zip_index_event)
            self.__is_uPdata_accessToken.clicked.connect(self.is_uPdata_accessToken_event)
            self.__is_uPdata_svn.clicked.connect(self.is_uPdata_svn_event)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化绑定事件 异常： {e}")

    def __init_threadWorker(self):
        """初始化线程"""
        log("ExcelToolsView-> __init_threadWorker")
        try:
            self.__print_worker = ThreadWorker_Signal(my_print.set_signal)
            self.__print_worker.log_signal.connect(self.append_text)
            self.__print_worker.to_end_signal.connect(self.to_end)
            self.__print_worker.start_signal.connect(self.start)
            self.__print_worker.end_signal.connect(self.end)
            self.__print_worker.add_list_signal.connect(self.add_list)
            self.__print_worker.start()
            self.__print_worker.quit()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化线程 异常： {e}")

    def __init_excel_tools_data(self):
        """初始化ExcelToolsData"""
        log("ExcelToolsView-> __init_excel_tools_data")
        try:
            self.__excel_tools_data = ExcelToolsData()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化ExcelToolsData 异常： {e}")

    def __init_data(self):
        """初始化工具配置设置"""
        log('ExcelToolsView-> __init_data')
        try:
            self.__init_QComboBox()
            self.__init_QCheckBox()
            self.add_list()
            self.__svn_old_index.setText(str(self.__excel_tools_data.svn_old_index))
            self.__svn_new_index.setText(str(self.__excel_tools_data.svn_new_index))
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化工具配置设置 异常： {e}")

    def __init_panel(self):
        """初始化窗口主题"""
        log("ExcelToolsView-> __init_panel")
        try:
            text = self.__excel_tools_data.themes
            themes_list = self.__excel_tools_data.themes_list
            if text not in themes_list:
                apply_stylesheet(self.__app, theme=themes_list[0], extra=self.__excel_tools_data.extra)
            elif text:
                apply_stylesheet(self.__app, theme=text, extra=self.__excel_tools_data.extra)
            else:
                apply_stylesheet(self.__app, theme=themes_list[0], extra=self.__excel_tools_data.extra)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化窗口主题 异常： {e}")

    def __init_style(self):
        """应用样式"""
        log("ExcelToolsView-> __init_style")
        try:
            self.__excel_tools_ui.setStyleSheet(self.__excel_tools_data.Style_qss)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 应用样式 异常： {e}")

    def __init_menuBar(self):
        """初始化主题菜单"""
        log("ExcelToolsView-> __init_menuBar")
        try:
            dark = self.__color_menu.addMenu('dark（黑暗主题）')
            light = self.__color_menu.addMenu('light（亮色主题）')
            for item in self.__excel_tools_data.themes_list:
                if 'dark' in item:
                    dark.addAction(item)
                else:
                    light.addAction(item)
            for key in self.__excel_tools_data.high_color_dict.keys():
                self.__high_menu.addAction(key)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化主题菜单 异常： {e}")

    def __init_QComboBox(self):
        """初始化下拉列表数据"""
        log('ExcelToolsView-> __init_QComboBox')
        try:
            def set_server_index(qComboBox, current_index):
                """设置服务器列表"""
                qComboBox.addItems(self.__excel_tools_data.server_name_list)
                qComboBox.setCurrentIndex(current_index)

            def set_export__index(qComboBox, current_index):
                """设置导表目录列表"""
                qComboBox.addItems(self.__excel_tools_data.export_dir_list)
                qComboBox.setCurrentIndex(current_index)

            # --------------------tal1-----------------
            set_server_index(self.__up_server_index, self.__excel_tools_data.up_server_index)
            set_server_index(self.__server_config_index, self.__excel_tools_data.server_config_index)
            set_export__index(self.__export_dir_index, self.__excel_tools_data.export_dir_index)
            # set_height_color_index(self.__high_color_index, self.__excel_tools_data.height_color_index)
            # --------------------tal2-----------------
            set_server_index(self.__up_server_index_2, self.__excel_tools_data.up_server_index_2)
            set_server_index(self.__server_config_index_2, self.__excel_tools_data.server_config_index_2)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化下拉列表数据 异常： {e}")

    def __init_QCheckBox(self):
        """初始化单选框数据"""
        log("ExcelToolsView-> __init_QCheckBox")
        try:
            # --------------------tal1-----------------
            self.__is_export_excel.setChecked(self.__excel_tools_data.is_export_excel)
            self.__is_down_config.setChecked(self.__excel_tools_data.is_down_config)
            self.__is_copy_file.setChecked(self.__excel_tools_data.is_copy_file)
            self.__is_up_data.setChecked(self.__excel_tools_data.is_up_data)
            # --------------------tal2-----------------
            self.__is_copy_file_2.setChecked(self.__excel_tools_data.is_copy_file_2)
            self.__is_zip_file.setChecked(self.__excel_tools_data.is_zip_file)
            self.__is_uPdata_accessToken.setChecked(self.__excel_tools_data.is_uPdata_accessToken)

            if self.__excel_tools_data.is_up_data:
                self.__push_Button.setEnabled(False)
                self.__server_config_list.setSelectionMode(QAbstractItemView.NoSelection)
            else:
                self.__push_Button.setEnabled(True)
                self.__server_config_list.setSelectionMode(QAbstractItemView.MultiSelection)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 初始化单选框数据 异常： {e}")

    def high_color_event(self, menu):
        """高亮设置响应事件"""
        log("ExcelToolsView-> high_color_event")
        try:
            text = menu.text()
            if text == self.__excel_tools_data.high_color_index:
                return
            self.__excel_tools_data.high_color_index = text
            self.__excel_tools_data.write_config()
            self.append_text(f"错误高亮颜色为【{self.__excel_tools_data.high_color_index}】", 1)
            log(self.__excel_tools_data.high_color_index)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 高亮设置响应事件 异常： {e}")

    def color_menu_event(self, menu):
        """主题颜色点击响应事件"""
        log("ExcelToolsView-> color_menu_event")
        try:
            text = menu.text()
            if text == "dark（黑暗主题）" or text == "light（亮色主题）":
                return
            if text != self.__excel_tools_data.themes:
                apply_stylesheet(self.__app, theme=text, extra=self.__excel_tools_data.extra)
                self.__excel_tools_data.themes = text
                self.__excel_tools_data.write_config()
                log(self.__excel_tools_data.themes)
                time.sleep(0.2)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 主题颜色点击响应事件 异常： {e}")

    def color_menu_hove_event(self, menu):
        """主题颜色鼠标覆盖事件"""
        log("ExcelToolsView-> color_menu_hove_event")
        try:
            text = menu.text()
            if text == "dark（黑暗主题）" or text == "light（亮色主题）":
                return
            apply_stylesheet(self.__app, theme=text, extra=self.__excel_tools_data.extra)
            log(self.__excel_tools_data.themes)
            time.sleep(0.2)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 主题颜色鼠标覆盖事件 异常： {e}")

    def color_menu_hide_event(self):
        """主题菜单栏隐藏响应事件"""
        log("ExcelToolsView-> color_menu_hide_event")
        try:
            apply_stylesheet(self.__app, theme=self.__excel_tools_data.themes)
            time.sleep(0.2)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 主题菜单栏隐藏响应事件 异常： {e}")

    def tabWidget_event(self, index):
        """切换tab事件"""
        log(f'ExcelToolsView-> tabWidget_event {index}')
        try:
            self.__excel_tools_data.set_tab(index)
            self.add_list()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 切换tab事件 异常： {e}")

    def up_server_index_event(self, index):
        """tab1 选择上传服务器"""
        log(f'ExcelToolsView-> up_server_index_event {index}')
        try:
            self.__excel_tools_data.up_server_index = index
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 up_server_index_event 异常： {e}")

    def server_config_index_event(self, index):
        """tab1 选择参考服"""
        log(f'ExcelToolsView-> server_config_index_event {index}')
        try:
            self.__excel_tools_data.server_config_index = index
        except Exception as e:
            self.prin_error()
            self.__message.show_message(
                f"****{os.path.basename(__file__)} >>> tab1 server_config_index_event 异常： {e}")

    def export_dir_index_event(self, index):
        """tab1 选择导表类型"""
        log(f'ExcelToolsView-> export_dir_index_event {index}')
        try:
            self.__excel_tools_data.export_dir_index = index
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 export_dir_index_event 异常： {e}")

    def is_export_excel_event(self, checked):
        """tab1 判断是否导表"""
        log(f'ExcelToolsView-> is_export_excel_event {checked}')
        try:
            self.__excel_tools_data.is_export_excel = checked
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 is_export_excel_event 异常： {e}")

    def is_up_data_event(self, checked):
        """tab1 判断是否上传"""
        log(f'ExcelToolsView-> is_up_data_event {checked}')
        try:
            self.__excel_tools_data.is_up_data = checked
            if self.__excel_tools_data.is_up_data:
                self.__push_Button.setEnabled(False)
                self.__server_config_list.clearSelection()
                self.__server_config_list.setSelectionMode(QAbstractItemView.NoSelection)
            else:
                self.__push_Button.setEnabled(True)
                self.__server_config_list.setSelectionMode(QAbstractItemView.MultiSelection)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 is_up_data_event 异常： {e}")

    def is_down_config_event(self, checked):
        """tab1 判断是否强制下载最新参考服配置列表"""
        log(f'ExcelToolsView-> is_down_config_event {checked}')
        try:
            self.__excel_tools_data.is_down_config = checked
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 is_down_config_event 异常： {e}")

    def is_copy_file_event(self, checked):
        """tab1 判断是否将导出来的json文件复制到工程"""
        log(f'ExcelToolsView-> is_copy_file_event {checked}')
        try:
            self.__excel_tools_data.is_copy_file = checked
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 is_copy_file_event 异常： {e}")

    def start_Button_event(self):
        """tab1 开始执行"""
        log('ExcelToolsView-> start_Button_event')
        try:
            self.__excel_tools_data.write_config()
            start_thread = threading.Thread(target=self.__excel_tools_data.start)
            start_thread.daemon = True
            start_thread.start()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 执行任务 异常： {e}")

    def push_Button_event(self):
        """tab1 上传配置"""
        log('ExcelToolsView-> push_Button_event')
        try:
            self.__excel_tools_data.write_config()
            selected_items = self.__server_config_list.selectedItems()
            if len(selected_items) > 0:
                for item in selected_items:
                    log(f"选择上传：{item.text()}")
                up_list = [item.text() for item in selected_items]
                self.__excel_tools_data.select_items = up_list
                start_thread = threading.Thread(target=self.__excel_tools_data.up_select_items)
                start_thread.daemon = True
                start_thread.start()
            else:
                self.__message.show_message("tips: 无选中项，请选择需要上传的json文件！")
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab1 手动选择上传配置 异常： {e}")

    def up_server_index_2_event(self, index):
        """tab2 选择上传服务器"""
        log(f'ExcelToolsView-> up_server_index_event {index}')
        try:
            self.__excel_tools_data.up_server_index_2 = index
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab2 up_server_index_2_event 异常： {e}")

    def server_config_index_2_event(self, index):
        """tab2 选择参考服"""
        log(f'ExcelToolsView-> server_config_index_2_event {index}')
        try:
            self.__excel_tools_data.server_config_index_2 = index
        except Exception as e:
            self.prin_error()
            self.__message.show_message(
                f"****{os.path.basename(__file__)} >>> tab2 server_config_index_2_event 异常： {e}")

    def svn_old_index_event(self):
        """tab2 svn旧版本"""
        log(f'ExcelToolsView-> svn_old_index_event{self.__svn_old_index.text()}')
        try:
            self.__excel_tools_data.svn_old_index = self.__svn_old_index.text()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab2 svn_old_index_event 异常： {e}")

    def svn_new_index_event(self):
        """tab2 svn新版本"""
        log(f'ExcelToolsView-> svn_new_index_event{self.__svn_new_index.text()}')
        try:
            self.__excel_tools_data.svn_new_index = self.__svn_new_index.text()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab2 svn_new_index_event 异常： {e}")

    def is_copy_file_2_event(self, checked):
        """tab2 json文件复制到工程事件"""
        log(f'ExcelToolsView-> is_copy_file_2_event {checked}')
        try:
            self.__excel_tools_data.is_copy_file_2 = checked
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab2 is_copy_file_2_event 异常： {e}")

    def is_zip_file_event(self, checked):
        """tab2 json文件压缩成zip格式事件"""
        log(f'ExcelToolsView-> is_zip_file_event {checked}')
        try:
            self.__excel_tools_data.is_zip_file = checked
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab2 is_zip_file_event 异常： {e}")

    def up_zip_index_event(self, index):
        """tab2 选择zip文件"""
        log(f'ExcelToolsView-> up_zip_index_event {index}')
        try:
            if index < 0:
                return
            self.__excel_tools_data.up_zip_index = self.__up_zip_index.currentText()
            self.append_text(f"选择zip文件：{self.__up_zip_index.currentText()}", 1)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> tab2 up_zip_index_event 异常： {e}")

    def is_uPdata_accessToken_event(self, checked):
        """tab2 是否更新accessToken"""
        log(f'ExcelToolsView-> is_uPdata_accessToken_event {checked}')
        try:
            self.__excel_tools_data.is_uPdata_accessToken = checked
        except Exception as e:
            self.prin_error()
            self.__message.show_message(
                f"****{os.path.basename(__file__)} >>> tab2 is_uPdata_accessToken_event 异常： {e}")

    def is_uPdata_svn_event(self, checked):
        """tab2 是否更新accessToken"""
        log(f'ExcelToolsView-> is_uPdata_svn_event {checked}')
        try:
            self.__excel_tools_data.is_uPdata_svn = checked
        except Exception as e:
            self.prin_error()
            self.__message.show_message(
                f"****{os.path.basename(__file__)} >>> tab2 is_uPdata_svn_event 异常： {e}")

    def hot_export_Button_event(self):
        """tab2 热更导表"""
        log('ExcelToolsView-> hot_export_Button_event')
        try:
            self.__excel_tools_data.write_config()
            hot_export_thread = threading.Thread(target=self.__excel_tools_data.hot_export)
            hot_export_thread.daemon = True
            hot_export_thread.start()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 热更导表 异常： {e}")

    def push_zip_Button_event(self):
        """tab2 上传zip文件"""
        log('ExcelToolsView-> push_zip_Button_event')
        try:
            self.__excel_tools_data.write_config()
            self.__excel_tools_data.push_hot_export()
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 上传zip文件 异常： {e}")

    def append_text(self, text: str, level: int = 0):
        """打印控件输出信息，包含高亮内容,text:需要输出的信息，level：日志级别（0：普通，1：提示，2：异常）"""
        # log('ExcelToolsView-> append_text')
        try:
            tab_print = self.__log_text
            if self.__excel_tools_data.tab == 1:
                tab_print = self.__log_text_2

            if not self.__excel_tools_data:
                tab_print.append(text)
            else:
                if level == 0:
                    tab_print.append(text)
                elif level == 1:
                    tab_print.append(self.__excel_tools_data.add_height(text))
                elif level == 2:
                    error_flag = self.__excel_tools_data.add_height(
                        "**************** ERROR ERROR ERROR ****************")
                    tab_print.append("")
                    tab_print.append(error_flag)
                    tab_print.append("")
                    _sp_str = text.split(", line")
                    log("======================================")
                    log(_sp_str)
                    log("======================================")
                    if len(_sp_str) > 1:
                        for i in range(len(_sp_str)):
                            if i > 0:
                                _sp_str[i] = self.__excel_tools_data.add_height(f"line{_sp_str[i]}")
                            else:
                                _sp_str[i] = self.__excel_tools_data.add_height(_sp_str[i])
                            tab_print.append(_sp_str[i])
                    log("======================================")
                    tab_print.append("")
                    tab_print.append(error_flag)
                    tab_print.append("")
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 打印控件输出信息 异常： {e}")

    def add_list(self):
        """teb1 添加服务器json文件到列表控件"""
        log('ExcelToolsView-> add_list')
        try:
            if self.__excel_tools_data.tab == 0:
                up_files_list = self.__excel_tools_data.get_dir_server_list()
                if len(up_files_list) == 0:
                    return
                self.append_text(f"config_server 文件夹中有：{len(up_files_list)} 文件")
                if self.__server_config_list.count() > 0:
                    self.__server_config_list.clear()
                self.__server_config_list.addItems(up_files_list)
            elif self.__excel_tools_data.tab == 1:
                if self.__excel_tools_data.zip_list is None or len(self.__excel_tools_data.zip_list) == 0:
                    self.__up_zip_index.setEnabled(False)
                    self.__push_zip_Button.setEnabled(False)
                else:
                    self.__up_zip_index.setEnabled(True)
                    self.__push_zip_Button.setEnabled(True)
                    if self.__up_zip_index.count() > 0:
                        self.__up_zip_index.clear()
                    self.__up_zip_index.addItems(self.__excel_tools_data.zip_list)
            time.sleep(0.3)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(
                f"****{os.path.basename(__file__)} >>> teb1 添加服务器json文件到列表控件 异常： {e}")

    def to_end(self):
        """打印框打印时，输入定位到行尾"""
        log('ExcelToolsView-> to_end')
        try:
            scrollbar = self.__log_scrollbar
            if self.__excel_tools_data.tab == 1:
                scrollbar = self.__log_scrollbar_2
            scrollbar.setValue(scrollbar.maximum())
            time.sleep(0.2)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 打印框打印时，输入定位到行尾 异常： {e}")

    def start(self):
        """开始执行时需要处理的界面事件"""
        log('ExcelToolsView-> start')
        try:
            if self.__excel_tools_data.tab == 0:
                self.__start_Button.setEnabled(False)
                self.__push_Button.setEnabled(False)
                self.__up_server_index.setEnabled(False)
                self.__server_config_index.setEnabled(False)
                self.__export_dir_index.setEnabled(False)
                self.__is_export_excel.setEnabled(False)
                self.__is_down_config.setEnabled(False)
                self.__is_copy_file.setEnabled(False)
                self.__is_up_data.setEnabled(False)
                self.__server_config_list.setSelectionMode(QAbstractItemView.NoSelection)
            elif self.__excel_tools_data.tab == 1:
                self.__up_server_index_2.setEnabled(False)
                self.__server_config_index_2.setEnabled(False)
                self.__svn_old_index.setEnabled(False)
                self.__svn_new_index.setEnabled(False)
                self.__is_copy_file_2.setEnabled(False)
                self.__is_zip_file.setEnabled(False)
                self.__hot_export_Button.setEnabled(False)
                self.__push_zip_Button.setEnabled(False)
                self.__up_zip_index.setEnabled(False)
                self.__is_uPdata_accessToken.setEnabled(False)
                self.__is_uPdata_svn.setEnabled(False)
            time.sleep(0.5)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 开始执行时需要处理的界面事件 异常： {e}")

    def end(self):
        """结束执行时需要处理的界面事件"""
        log('ExcelToolsView-> end')
        try:
            if self.__excel_tools_data.tab == 0:
                if self.__excel_tools_data.is_up_data:
                    self.__push_Button.setEnabled(False)
                    self.__server_config_list.setSelectionMode(QAbstractItemView.NoSelection)
                else:
                    self.__push_Button.setEnabled(True)
                    self.__server_config_list.setSelectionMode(QAbstractItemView.MultiSelection)
                self.__start_Button.setEnabled(True)
                self.__up_server_index.setEnabled(True)
                self.__server_config_index.setEnabled(True)
                self.__export_dir_index.setEnabled(True)
                self.__is_export_excel.setEnabled(True)
                self.__is_up_data.setEnabled(True)
                self.__is_down_config.setEnabled(True)
                self.__is_copy_file.setEnabled(True)
            elif self.__excel_tools_data.tab == 1:
                self.__up_server_index_2.setEnabled(True)
                self.__server_config_index_2.setEnabled(True)
                self.__svn_old_index.setEnabled(True)
                self.__svn_new_index.setEnabled(True)
                self.__is_copy_file_2.setEnabled(True)
                self.__is_zip_file.setEnabled(True)
                self.__hot_export_Button.setEnabled(True)
                self.__is_uPdata_accessToken.setEnabled(True)
                self.__is_uPdata_svn.setEnabled(True)
                if self.__excel_tools_data.is_uPdata_svn:
                    self.__svn_old_index.setText(str(self.__excel_tools_data.svn_old_index))
            time.sleep(0.5)
        except Exception as e:
            self.prin_error()
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> 结束执行时需要处理的界面事件 异常： {e}")

    def prin_error(self):
        """获取错误信息堆栈，并且使用高亮颜色输出"""
        error_details = traceback.format_exc()
        log(error_details)
        try:
            if not self.__excel_tools_data:
                self.__message.show_message(
                    f"****{os.path.basename(__file__)} >>> prin_error 异常：excel_tools_data为空 ")
                return
            if self.__excel_tools_data.tab == 0:
                self.append_text(f"异常：{error_details}", 2)
            elif self.__excel_tools_data.tab == 1:
                self.append_text(f"异常：{error_details}", level=2)
            else:
                self.append_text(f"异常：{error_details}", 2)
        except Exception as e:
            self.__message.show_message(f"****{os.path.basename(__file__)} >>> prin_error 异常： {e}")

    def show(self):
        """展示窗口"""
        try:
            self.__excel_tools_ui.show()
        except Exception as e:
            raise e
